package com.blog.cmrpersonalblog.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.blog.cmrpersonalblog.common.Result;
import com.blog.cmrpersonalblog.dto.notification.requset.NotificationSettingsUpdateRequest;
import com.blog.cmrpersonalblog.dto.notification.respnose.NotificationSettingsResponse;
import com.blog.cmrpersonalblog.service.NotificationSettingsService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 通知设置控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/notification-settings")
public class NotificationSettingsController {

    @Resource
    private NotificationSettingsService notificationSettingsService;

    /**
     * 获取当前用户的通知设置
     *
     * @return 通知设置
     */
    @GetMapping
    @SaCheckLogin
    public Result<NotificationSettingsResponse> getUserSettings() {
        try {
            Long userId = StpUtil.getLoginIdAsLong();
            NotificationSettingsResponse settings = notificationSettingsService.getUserSettings(userId);
            return Result.success("查询成功", settings);
        } catch (Exception e) {
            log.error("获取通知设置失败", e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 更新当前用户的通知设置
     *
     * @param request 更新请求
     * @return 操作结果
     */
    @PutMapping
    @SaCheckLogin
    public Result<String> updateUserSettings(@Valid @RequestBody NotificationSettingsUpdateRequest request) {
        try {
            Long userId = StpUtil.getLoginIdAsLong();
            boolean success = notificationSettingsService.updateUserSettings(userId, request);
            return success ? Result.success("更新成功") : Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新通知设置失败", e);
            return Result.error("更新失败：" + e.getMessage());
        }
    }

    /**
     * 重置当前用户的通知设置为默认值
     *
     * @return 操作结果
     */
    @PostMapping("/reset")
    @SaCheckLogin
    public Result<String> resetUserSettings() {
        try {
            Long userId = StpUtil.getLoginIdAsLong();
            boolean success = notificationSettingsService.resetUserSettings(userId);
            return success ? Result.success("重置成功") : Result.error("重置失败");
        } catch (Exception e) {
            log.error("重置通知设置失败", e);
            return Result.error("重置失败：" + e.getMessage());
        }
    }

    /**
     * 快捷开关：一键开启所有通知
     *
     * @return 操作结果
     */
    @PostMapping("/enable-all")
    @SaCheckLogin
    public Result<String> enableAllNotifications() {
        try {
            Long userId = StpUtil.getLoginIdAsLong();
            NotificationSettingsUpdateRequest request = new NotificationSettingsUpdateRequest();
            request.setFollowEnabled(1);
            request.setLikeEnabled(1);
            request.setCommentEnabled(1);
            request.setReplyEnabled(1);
            request.setMentionEnabled(1);
            request.setSystemEnabled(1);
            request.setCollectEnabled(1);
            
            boolean success = notificationSettingsService.updateUserSettings(userId, request);
            return success ? Result.success("已开启所有通知") : Result.error("操作失败");
        } catch (Exception e) {
            log.error("开启所有通知失败", e);
            return Result.error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 快捷开关：一键关闭所有通知
     *
     * @return 操作结果
     */
    @PostMapping("/disable-all")
    @SaCheckLogin
    public Result<String> disableAllNotifications() {
        try {
            Long userId = StpUtil.getLoginIdAsLong();
            NotificationSettingsUpdateRequest request = new NotificationSettingsUpdateRequest();
            request.setFollowEnabled(0);
            request.setLikeEnabled(0);
            request.setCommentEnabled(0);
            request.setReplyEnabled(0);
            request.setMentionEnabled(0);
            request.setSystemEnabled(0);
            request.setCollectEnabled(0);
            
            boolean success = notificationSettingsService.updateUserSettings(userId, request);
            return success ? Result.success("已关闭所有通知") : Result.error("操作失败");
        } catch (Exception e) {
            log.error("关闭所有通知失败", e);
            return Result.error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 快捷开关：开启/关闭免打扰模式
     *
     * @param enabled 是否开启
     * @param startTime 开始时间（格式：HH:mm:ss）
     * @param endTime 结束时间（格式：HH:mm:ss）
     * @return 操作结果
     */
    @PostMapping("/do-not-disturb")
    @SaCheckLogin
    public Result<String> toggleDoNotDisturb(
            @RequestParam Integer enabled,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        try {
            Long userId = StpUtil.getLoginIdAsLong();
            NotificationSettingsUpdateRequest request = new NotificationSettingsUpdateRequest();
            request.setDoNotDisturbEnabled(enabled);
            
            if (enabled == 1) {
                if (startTime == null || endTime == null) {
                    return Result.error("开启免打扰模式需要设置时间段");
                }
                request.setDoNotDisturbStart(startTime);
                request.setDoNotDisturbEnd(endTime);
            }
            
            boolean success = notificationSettingsService.updateUserSettings(userId, request);
            return success ? Result.success(enabled == 1 ? "已开启免打扰模式" : "已关闭免打扰模式") 
                          : Result.error("操作失败");
        } catch (Exception e) {
            log.error("切换免打扰模式失败", e);
            return Result.error("操作失败：" + e.getMessage());
        }
    }
}

